﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public class RoyalItemHistoriesBLL : IRoyalItemHistoriesBLL
    {
        private static readonly IRoyalItemHistoriesDAL DAL = DataAccess.CreateRoyalItemHistories();

        private EmployeeInfo empInfo = new EmployeeInfo();
        private RoyalItemInfo roiInfo = new RoyalItemInfo();
        private RoyalItemHistoryInfo hisInfo = new RoyalItemHistoryInfo();

        public IList<RoyalItemHistoryInfo> ListRoyalItemHistories(string Employee)
        {
            if (Employee.Length > 0)
                empInfo.Sequence = Int32.Parse(Employee);
            hisInfo.Employee = empInfo;

            return DAL.ListRoyalItemHistories(hisInfo);
        }
        public IList<RoyalItemHistoryInfo> ListRoyalItemHistoriesByYear(string Year)
        {
            if (Year.Length > 0)
                hisInfo.ReceiveDate = DateTime.Parse("01/01/" + Year);
            else
                hisInfo.ReceiveDate = (DateTime?)null;

            return DAL.ListRoyalItemHistories(hisInfo);
        }
        public IList<RoyalItemHistoryInfo> ListRoyalItemHistoriesBySequence(int Sequence)
        {
            return DAL.ListRoyalItemHistories(new RoyalItemHistoryInfo(Sequence));
        }
        public DataSet LoadRoyalItemHistories(Nullable<int> Sequence, Nullable<int> EmpSequence, Nullable<int> ReceiveYear)
        {
            hisInfo.Sequence = 0;
            hisInfo.ReceiveDate = (DateTime?)null;

            if (Sequence.HasValue)
                hisInfo.Sequence = (int)Sequence;

            if (ReceiveYear.HasValue)
                hisInfo.ReceiveDate = DateTime.Parse("01/01/" + ReceiveYear.ToString());

            if (EmpSequence.HasValue)
            {
                empInfo.Sequence = (int)EmpSequence;
                hisInfo.Employee = empInfo;
            }                

            return DAL.LoadRoyalItemHistories(hisInfo);
        }
        public void Insert(string Employee, Nullable<int> RoyalItemReqSeq, string RoyalItem, Nullable<DateTime> ReceiveDate
                , string PosRef, string PosName, string ClassName, Nullable<Double> Salary, string JobSiteName
                , string GazetteNo1, string GazetteNo2, string GazetteNo3, string GazetteNo4
                , string GazetteNo5, Nullable<DateTime> GazetteDate, Nullable<DateTime> RoyalDate, int CreatedBy)
        {
            if (Employee.Length > 0)
                empInfo.Sequence = Int32.Parse(Employee);
            if (RoyalItem.Length > 0)
                roiInfo.Sequence = Int32.Parse(RoyalItem);

            try
            {
                DAL.Insert(new RoyalItemHistoryInfo(0, empInfo, RoyalItemReqSeq, roiInfo, ReceiveDate, PosRef, PosName
                    , ClassName, Salary, JobSiteName, GazetteNo1, GazetteNo2, GazetteNo3, GazetteNo4, GazetteNo5
                    , GazetteDate, RoyalDate, (DateTime?)null, 0, "", CreatedBy, 0));
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Update(int Sequence, string Employee, Nullable<int> RoyalItemReqSeq, string RoyalItem, Nullable<DateTime> ReceiveDate
                , string PosRef, string PosName, string ClassName, Nullable<Double> Salary, string JobSiteName
                , string GazetteNo1, string GazetteNo2, string GazetteNo3
                , string GazetteNo4, string GazetteNo5, Nullable<DateTime> GazetteDate, Nullable<DateTime> RoyalDate
                , int UpdatedBy)
        {
            if (Employee.Length > 0)
                empInfo.Sequence = Int32.Parse(Employee);
            if (RoyalItem.Length > 0)
                roiInfo.Sequence = Int32.Parse(RoyalItem);

            try
            {
                DAL.Update(new RoyalItemHistoryInfo(Sequence, empInfo, RoyalItemReqSeq, roiInfo, ReceiveDate, PosRef, PosName
                    , ClassName, Salary, JobSiteName, GazetteNo1, GazetteNo2, GazetteNo3, GazetteNo4, GazetteNo5
                    , GazetteDate, RoyalDate, (DateTime?)null, 0, "", 0, UpdatedBy));
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void UpdateHistory(int Sequence, string RoyalItem, Nullable<DateTime> ReceiveDate
                , string GazetteNo1, string GazetteNo2, string GazetteNo3, string GazetteNo4, string GazetteNo5
                , Nullable<DateTime> GazetteDate, Nullable<DateTime> RoyalDate, int UpdatedBy)
        {
            if (RoyalItem.Length > 0)
                roiInfo.Sequence = Int32.Parse(RoyalItem);

            try
            {
                DAL.UpdateHistory(new RoyalItemHistoryInfo(Sequence, empInfo, (int?)null, roiInfo, ReceiveDate, "", ""
                    , "", 0, "", GazetteNo1, GazetteNo2, GazetteNo3, GazetteNo4, GazetteNo5
                    , GazetteDate, RoyalDate, (DateTime?)null, 0, "", 0, UpdatedBy));
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                DAL.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
    }
}
